package com.qf.servlet;


import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.qf.entity.Book;
import com.qf.entity.BookVo;
import com.qf.service.BookService;

import com.qf.service.impl.BookServiceImpl;
import com.qf.utils.PageUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@WebServlet(name = "bookServlet", urlPatterns = "/bookServlet")
public class BookServlet extends BaseServlet {
    private BookService bookService;
    private Book book;

    @Override
    public void init(ServletConfig config) throws ServletException {
        bookService = new BookServiceImpl();
        book = new Book();
    }

    public void selectPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        String bname = req.getParameter("bname");
        String state = req.getParameter("state");
        String startTime = req.getParameter("start_time");
        String endTime = req.getParameter("end_time");
        String currentPageNum = req.getParameter("currentPageNum");
        String pageSize = req.getParameter("pageSize");

        BookVo bookVo = new BookVo(bname, state, startTime, endTime);

        PageUtils<Book> bookPage = bookService.selectPage(bookVo, currentPageNum, pageSize);
        String s = JSON.toJSONString(bookPage);
        pw.print(s);

        pw.close();
    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        String cid = req.getParameter("cid");
        String bname = req.getParameter("bname");
        String author = req.getParameter("author");
        String price = req.getParameter("price");
        String description = req.getParameter("description");
        String version = req.getParameter("version");
        String state = req.getParameter("state");
        String bookDate = req.getParameter("book_date");

        book.setCid(Integer.valueOf(cid));
        book.setBname(bname);
        book.setAuthor(author);
        book.setPrice(Double.valueOf(price));
        book.setDescription(description);
        book.setVersion(version);
        book.setState(Integer.valueOf(state));
        book.setBook_date(LocalDateTime.parse(bookDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        int num = bookService.add(book);
        if (num > 0) {
            pw.write("<script>alert('添加成功');window.location.href='bookList.html'</script>");
        } else {
            pw.write("<script>alert('添加失败')</script>");
        }
        pw.close();
    }

    public void getBookInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        String bid = req.getParameter("bid");
        Book book = bookService.selectBook(Integer.valueOf(bid));
        pw.print(JSON.toJSONString(book));
        pw.close();
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        String bid = req.getParameter("bid");
        String cid = req.getParameter("cid");
        String bname = req.getParameter("bname");
        String author = req.getParameter("author");
        String price = req.getParameter("price");
        String description = req.getParameter("description");
        String version = req.getParameter("version");
        String state = req.getParameter("state");
        String bookDate = req.getParameter("book_date");
        book.setBid(Integer.parseInt(bid));
        book.setCid(Integer.parseInt(cid));
        book.setBname(bname);
        book.setAuthor(author);
        book.setPrice(Double.valueOf(price));
        book.setDescription(description);
        book.setVersion(version);
        book.setState(Integer.parseInt(state));

        book.setBook_date(LocalDateTime.parse(bookDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        int num = bookService.update(book);
        if (num > 0) {
            pw.write("<script>alert('修改成功');window.location.href='bookList.html'</script>");
        } else {
            pw.write("<script>alert('修改失败')</script>");
        }
        pw.close();
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        String ids = req.getParameter("ids");
        int num = bookService.delete(ids);

        pw.print(num > 0);
        pw.close();
    }

}
